<!DOCTYPE html>
<html>
	<head>
		<#include "/templates/commons/head.html">
		<script type="text/javascript">
		    /**
		     *点击添加按钮进入添加页面
		     */
		    function save() {
				if (!$("#addForm").validateForm()) {
				    return;
				}
				if (seleBox("roleIds")) {
				    var method = document.addForm.method.value;
				    var messages = "";
				    if (method == "addSave" || method == "editSave") {
						var retObj = new Object();
						var params = {
						    "param_userName" : $("#accound").val()
						};
						var url = "${ctx!}/UserController/checkReId.do?param_date="
							+ new Date().getTime();
						
						var a = "";
						Common.network(url, "POST", params, false, function(data) {
						    a = data;
						})
						var b = $("#realUserId").val();
						if (method == "addSave" && a != "" && a != undefined) {
						    layer.msg("帐号已存在，请重新输入！");
						    return;
						}
				    }
				    if (method == "editSave") {
						if (a != "" && a != b) {
						    layer.msg("帐号已存在，请重新输入！");
						    return;
						}
				    }
				    if (method == "addSave") {
						messages = "您是否确定要添加？";
				    } else if (method == "editSave") {
						messages = "您是否确定要修改？";
				    }
				    layer.confirm(messages, {
						btn : [ '确定', '取消' ]
					    }, function() {
							$("#cityCode").val($("#city").val() != "" ? $("#city").val() : $("#province").val());
							document.addForm.action = "${ctx!}/UserController/" + method + ".do";
							document.addForm.submit();
					    }, function() {
				    });
				}
		    }
		    /**
		     *点击返回按钮进入用户列表页面
		     */
		    function goBack() {
				document.addForm.action = "${ctx!}/UserController/queryList.do";
				document.addForm.submit();
		    }
		    /**
		     * 更换所属机构变更角色信息
		     */
			function changeOrgFun(data) {
			    var url = "${ctx!}/UserController/getRoleListByOrg.do"
				var params = {"orgId":data.value};
				var retObj = new Object();
				Common.postNetwork(url, params, function(data) {
				    if (data != null) {
						$(".roleList").empty();
						$.each(data, function(i, o) {
						    $(".roleList").append('<input type="checkbox" name="roleIds" value="' + o.code + '" title="' + o.name + '" />');
						});
						form.render();
				    }
				});
			}
		    
			//省市联动
			function initCity() {
			    var cityCodePath = "<@city cityPath="${userBean.cityCode!''}">${cityPath}</@city>";
			    if (Common.isEmpty(cityCodePath)) {
					return;
			    }
			    var cityCodes = cityCodePath.split(",");
			    $.each(cityCodes, function(i, o) {
				    if (o == null || o == "" || o == " ") {
						cityCodes.splice(i, 1);
				    }
				});
			    var provinceCode = "";
			    var cityCode = "";
			    if (cityCodes.length >= 1) {
					provinceCode = cityCodes[0];
			    }
			    if (cityCodes.length >= 2) {
					cityCode = cityCodes[1]
			    }
			    if (!Common.isEmpty(provinceCode)) {
					Common.initSelect(document.addForm.province, provinceCode);
			    }
			    if (!Common.isEmpty(cityCode)) {
					Common.changeRegion(document.addForm.city, provinceCode)
					Common.initSelect(document.addForm.city, cityCode);
					form.render();
			    }
			}
			
		    function ready() {
				//初始化selectchange事件
				form.on('select(changeOrg)', function(data) {
				    changeOrgFun(data);
				});
				initCity()
				form.render();
		    }
		
		    function seleBox(boxName) {
				var groups = document.getElementsByName(boxName);
				if (groups) {
				    var min = 1, max = groups.length;
				    if (!mustChecked(groups, min, max)) {
						layer.msg("请为用户分配角色！");
						return false;
				    }
				}
				return true;
		    }
		
		    function chooseDialog() {
				chooseDocument(0, "userHead", function(res) {
				    console.log(res);
				    $("#head-img").val(res[0].fileId);
				    $("#LAY_upload").attr("src", res[0].pathShow).removeClass("fn-hide");
				    $(".upload-btn").addClass("upload-btn-finish");
				});
		    }
		</script>
	</head>
	<body>
		<div id="div-body" class="body">
			<div class="hid-scroller-body">
				<!-- 标题 Start -->
				<#include "/templates/commons/menuPath.html">
				<!-- 标题 End -->
				<form class="layui-form" id="addForm" name="addForm" method="post">
					<!-- list页面搜索条件 Start -->
					<#if method == 'add'>
						<input type="hidden" name="method" value="addSave" />
					</#if>
					<#if method == 'edit'>
						<input type="hidden" name="method" value="editSave" />
					</#if>
					<input type="hidden" name="oldAccount" value="${userBean.account!}" />
					<input type="hidden" name="userId" value="${userBean.userId!}"/>
					<input type="hidden" name="cityCode" value="${userBean.cityCode!}"/>
					<input type="hidden" name="param_userId" value="${Request.param_userId!}" />
					<input type="hidden" id="param_userName" name="param_userName" value="${Request.param_userName!}" />
					<!-- list页面搜索条件 End -->
					<!-- 添加信息 Form Start -->
					<div class="main-container">
						<div class="layui-form-item">
							<label class="layui-form-label">头像</label>
							<div class="layui-input-block" style="min-height: 90px;">
								<div class="site-demo-upload upload-file-btn-group">
									<#if userBean.headImgPath??>
										<#assign var_class = 'fn-show'/>
									<#else>
										<#assign var_class = 'fn-hide'/>
									</#if>
									<img id="LAY_upload" src="${userBean.headImgPathShow!''}" class="${var_class}">
									<span class="file-name"></span>
									<button type="button" class="layui-btn layui-btn-mini layui-btn-danger upload-btn <#if userBean.headImgPathShow??>upload-btn-finish</#if>" onclick="chooseDialog();"><i class="layui-icon">&#xe67c;</i>上传图片</button>
								</div>
								<input type="hidden" name="headImg" value="${userBean.headImg!}" id="head-img"/>
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label"><span class="redSpan">*</span>帐号</label>
							<div class="layui-input-block">
								<input type="text" name="account" value="${userBean.account!}" id="accound" isValidate="true" notEmpty="true" leng="20" isCharOrNum="true" msg="帐号" placeholder="请输入账号" class="layui-input" lay-verify="title" />
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label"><span class="redSpan">*</span>姓名</label>
							<div class="layui-input-block">
								<input type="text" name="userName" value="${userBean.userName!}" id="userName" isValidate="true" notEmpty="true" leng="20" msg="姓名" placeholder="请输入姓名" class="layui-input" lay-verify="title"/>
							</div>
						</div>
						<#if method == 'add'>
							<div class="layui-form-item">
								<label class="layui-form-label"><span class="redSpan">*</span>密码</label>
								<div class="layui-input-block">
									<input type="password" name="password" value="" isValidate="true" notEmpty="true" isCharOrNum="true" leng="32" msg="密码" placeholder="请输入密码" class="layui-input" lay-verify="title"/>
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label"><span class="redSpan">*</span>确认密码</label>
								<div class="layui-input-block">
									<input type="password" path="makeSurePassword" value="" isValidate="true" notEmpty="true" confirmSameValue="password" msg="确认密码" placeholder="请确认密码" class="layui-input" lay-verify="title" />
								</div>
							</div>
						</#if>
						<div class="layui-form-item">
							<label class="layui-form-label"><span class="redSpan">*</span>所属机构</label>
							<div class="layui-input-inline">
								<#if method=='add'>
									<select name="orgId" lay-filter="changeOrg">
										<option value="">-请选择机构-</option>
										<#if orgList??>
											<#list orgList as item>
												<option value="${item.orgId}" <#if item.orgId == userBean.orgId!''>selected</#if>>${item.orgName}</option>
											</#list>
										</#if>
									</select>
								</#if>
								<#if method == 'edit'>
									<#if orgList??>
										<#list orgList as item>
											<#if item.orgId == userBean.orgId!''>
												<span class="layui-input">${item.orgName!''}</span>
												<input type="hidden" name="orgId" value="${item.orgId!''}"/>
												<#break>
											</#if>
										</#list>
									</#if>
								</#if>
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">省市</label>
							<div class="layui-input-inline">
								<select name="province" id="province" lay-filter="changeRegions">
									<option value="">-请选择省份-</option>
									<@city pcityCode="0">
										<#list cityList as item>
											<option value="${item.code}">${item.name}</option>
										</#list>
									</@city>
								</select>
							</div>
							<div class="layui-input-inline">
								<select name="city" id="city">
									<option value="">-请选择-</option>
								</select>
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">性别</label>
							<div class="layui-input-block">
								<@dict type="sex">
									<#if dictList?exists>
										<#list dictList as dict>
											<input type="radio" name="sex" value="${dict.code}"  title="${dict.name}" <#if (userBean.sex?? && dict.code == userBean.sex!'')>checked</#if>/>
										</#list>
									</#if>
								</@dict>
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">联系电话</label>
							<div class="layui-input-block">
								<input type="text" name="mobile" value="${userBean.mobile!}" isValidate="true" isMobile="true" maxlength="20" placeholder="请输入联系电话(手机)" class="layui-input" lay-verify="title"/>
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">座机</label>
							<div class="layui-input-block">
								<input type="text" name="tel" value="${userBean.tel!}" isValidate="true" isPhone="true" placeholder="请输入座机电话" class="layui-input" lay-verify="title"/>
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">邮箱</label>
							<div class="layui-input-block">
								<input type="text" name="email" value="${userBean.email!}" isValidate="true" isEmail="true" leng="50" placeholder="请输入邮箱" class="layui-input" lay-verify="title"/>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label"><span class="redSpan">*</span>角色信息</label>
								<div class="layui-input-block roleList">
									<#if roleList?exists>
										<#list roleList as baseBean>
											<input type="checkbox" name="roleIds" isValidate="true" notEmpty="true" <#if userBean.roleIds??>${userBean.roleIds?seq_contains(baseBean.code!'')?string("checked", "")}</#if> value="${baseBean.code!}" title="${baseBean.name!}" />
										</#list>
									</#if>
								</div>
							</div>
						</div>
						<div class="main-button">
							<div class="layui-input-block">
								<#if (Session.CUR_MENU.addRole==1 && method=='add') || (Session.CUR_MENU.updateRole==1 && method=='edit')>
									<button type="button" class="layui-btn" onClick="save();"><i class="layui-icon">&#xe642;</i>保存</button>
								</#if>
								<button type="button" class="layui-btn layui-btn-primary" onClick="goBack();"><i class="layui-icon">&#xe65c;</i>返回</button>
							</div>
						</div>
					</div>
				</form>
			</div>
		</div>
	</body>
</html>